/*
 * @lc app=leetcode.cn id=20 lang=javascript
 *
 * [20] 有效的括号
 */

// @lc code=start
/**
 * @param {string} s
 * @return {boolean}
 */
var isValid = function (s) {
  let count = s.length / 2
  for (let i = 0; i < count; i++) {
    s = s.replace(/{}|\[\]|\(\)/g, '')
    if (s.length == 0) {
      return true
    }
  }
  return false
}
// @lc code=end

// console.log(isValid('{[]}'))
// console.log(isValid('([)]'))
// console.log(isValid('(]'))
console.log(isValid('[({(())}[()])]'))

// 优秀答案

/**
 * @param {string} s
 * @return {boolean}
 */
var isValid2 = function (s) {
  const stack = []
  for (let i = 0; i < s.length; i++) {
    switch (s[i]) {
      case '(':
        stack.push(')')
        break
      case '{':
        stack.push('}')
        break
      case '[':
        stack.push(']')
        break
      case stack[stack.length - 1]:
        stack.pop()
        break
      default:
        return false
    }
  }

  return stack.length === 0
}
